﻿jQuery.fn.disable = function()
{
    return this.each(function()
    {
        if (typeof this.disabled != 'undefined') this.disabled = true;
    });
}
jQuery.fn.enable = function()
{
    return this.each(function()
    {
        if (typeof this.disabled != 'undefined') this.disabled = false;
    });
}

jQuery.fn.anySelected = function()
{
    for (var i = 0; i < this.length; ++i)
    {
        if (typeof this[i].selected != 'undefined' && this[i].selected)
            return true;
    }
    return false;
}

jQuery.fn.setFocus = function()
{
    if (this.length >0)
        this[0].focus();
    return this;
}


String.prototype.format = function()
{
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture) { return args[capture.match(/\d+/)]; });
}

jQuery.fn.defaultButton = function(sel)
{
    if (typeof (sel) == "undefined")
    {
        sel = $(this).attr("defaultbutton");
        if (typeof (sel) == "undefined")
            return;
        sel = "#" + sel;
    }
    var ButtonKeys = { "EnterKey": 13 };
    $(this).keypress(function(evnt)
    {
        if (evnt.which == ButtonKeys.EnterKey)
        {
            $(sel, this).click();
            return false;
        }
    });
}
